﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ServiceModel;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using KMT_Pro.Web;
using KMT_Pro.Views.HardwareViews;
using SilverlightCommands;

namespace KMT_Pro.ViewModels.HardwareViewModels
{
    public class HardwareMasterViewModel : MessageSendableViewModel
    {
        #region Constants and Fields

        CustomerContext _context;

        private ObservableCollection<SP_HardwareMaster_Select_Result> HardwareList = new ObservableCollection<SP_HardwareMaster_Select_Result>();

        private PagedCollectionView hardwareCollection;

        private SP_HardwareMaster_Select_Result selectedHardwareMaster;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 생성자
        /// </summary>
        public HardwareMasterViewModel()
        {
            this.SearchClick = new RelayCommand((p) =>
            {
                try
                {
                    IsBusy = true;

                    HardwareList.Clear();
                    HardwareCollection = new PagedCollectionView(this.HardwareList);
                    _context = new CustomerContext();
                    var q = _context.Load(_context.GetHardwareMaster_SelectQuery());

                    q.Completed += new EventHandler(SearchComplete);
                }
                catch (Exception)
                { }
            });

            this.editClick = new RelayCommand((p) =>
            {
                try
                {
                    ChildWindow HardwareDetailInfoView = new HardwareDetailInfoView(this.SelectedHardwareMaster.HardwareID.Trim()
                                                                            , this.SelectedHardwareMaster.HardwareName.Trim()
                                                                            , this.SelectedHardwareMaster.HardwareType.Trim()
                                                                            , this.SelectedHardwareMaster.CompanyName.Trim()
                                                                            , this.SelectedHardwareMaster.CompanyTelNum.Trim()
                                                                            , this.SelectedHardwareMaster.ASlimit.Trim()
                                                                            , this.SelectedHardwareMaster.etc.Trim()
                                                                            , true);
                    HardwareDetailInfoView.Closed += new EventHandler(HardwareDetailInfoView_Closed);
                    HardwareDetailInfoView.Show();
                }
                catch (Exception)
                { }
            });

            this.delClick = new RelayCommand((p) =>
            {
                try
                {
                    if (MessageBox.Show(this.SelectedHardwareMaster.HardwareName.Trim() + " => 정말 삭제하시겠습니까?", "경고!", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                    {
                        return;
                    }

                    this.IsBusy = true;

                    _context = new CustomerContext();

                    var q = _context.Load(_context.HardwareMaster_DeleteQuery(this.SelectedHardwareMaster.HardwareID.Trim()));
                    q.Completed += DeleteComplete;
                }
                catch (Exception ex)
                {
                    this.IsBusy = false;
                    ChildWindow errorWin = new ErrorWindow(ex);
                    errorWin.Show();
                }
            });

            this.NewClick = new RelayCommand((p) =>
            {
                try
                {
                    ChildWindow HardwareDetailInfoView = new HardwareDetailInfoView("", "", "", "", "", "", "", false);
                    HardwareDetailInfoView.Closed += new EventHandler(HardwareDetailInfoView_Closed);
                    HardwareDetailInfoView.Show();
                }
                catch (Exception)
                { }
            });

            this.ExcelCommand = new RelayCommand((p) =>
                {
                    
                });
        }

        #endregion

        #region Properties

        // ------------------------ 속성 : Start -----------------------------------

        public PagedCollectionView HardwareCollection
        {
            set
            {
                this.hardwareCollection = value;
                this.OnPropertyChanged("HardwareCollection");
            }

            get
            {
                return hardwareCollection;
            }

        }

        public SP_HardwareMaster_Select_Result SelectedHardwareMaster
        {
            set
            {
                this.selectedHardwareMaster = value;
                this.OnPropertyChanged("SelectedHardwareMaster");
            }

            get
            {
                return selectedHardwareMaster;
            }

        }

        // ------------------------ 속성 : End -------------------------------------

        //------------------------- 버튼 : Start -----------------------------------

        public ICommand SearchClick { get; set; }

        public ICommand editClick { get; set; }

        public ICommand delClick { get; set; }

        public ICommand NewClick { get; set; }

        public ICommand ExcelCommand { get; set; }

        //------------------------- 버튼 : End -------------------------------------
        
        #endregion

        #region Event

        public void SearchComplete(object sender, EventArgs e)
        {
            try
            {
                LoadOperation lo = sender as LoadOperation;
                foreach (SP_HardwareMaster_Select_Result GAP in lo.AllEntities)
                {
                    HardwareList.Add(GAP);
                }
                HardwareCollection = new PagedCollectionView(this.HardwareList);
                IsBusy = false;
            }
            catch (Exception)
            { }
            
        }

        public void DeleteComplete(object sender, EventArgs e)
        {
            LoadOperation lo = sender as LoadOperation;
            try
            {
                foreach (ResultMessage GAP in lo.AllEntities)
                {
                    MessageBox.Show(GAP.ExceptionMessage);
                    this.IsBusy = false;
                    this.SearchClick.Execute(null);
                }
            }
            catch (Exception)
            { this.IsBusy = false; }
        }

        void HardwareDetailInfoView_Closed(object sender, EventArgs e)
        {
            try
            {
                HardwareDetailInfoView lw = (HardwareDetailInfoView)sender;

                if (lw.DialogResult == true)
                {
                    this.SearchClick.Execute(null);
                }
            }
            catch(Exception)
            { }
        }

        #endregion          

        #region Method

        #endregion
    }
}
